package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;
import org.assertj.core.util.Strings;

import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * Created by mz on 25/7/17.
 *
 */
@Data
public class RetailBreedRecord implements Serializable{

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = -5574994968228333170L;

    /**
     * 主键id
     */
    private Long id;

    /**
     * 单据编号
     */
    private String number;

    /**
     * 村ID，可以为空
     */
    private Long villageId;

    /**
     * 村，可以为空
     */
    private String village;

    /**
     * 畜主
     */
    private String name;

    /**
     * 畜主电话
     */
    private String mobile;

    /**
     * 养殖场id
     */
    private Long breedFactoryId;

    /**
     * 养殖场名称
     */
    private String breedFactoryName;

    /**
     * 畜种
     */
    private Integer breedType;

    /**
     * 二级畜种
     */
    private Integer breedSecondType;

    /**
     * 畜种单位
     */
    private Integer breedUnit;

    /**
     * 做单时间
     */
    private Date date;

    /**
     * 仔存栏
     */
    private Integer yongStock;

    /**
     * 肉存栏
     */
    private Integer meatStock;

    /**
     * 公存栏
     */
    private Integer maleStock;

    /**
     * 母存栏
     */
    private Integer femaleStock;

    /**
     * 原存栏
     */
    private Integer originStock;

    /**
     * 现存栏
     */
    private Integer stock;

    /**
     * 圈舍面积
     */
    private Double area;

    /**
     * 变动数
     */
    private Integer changeQuantity;

    /**
     * 死淘数
     */
    private Integer deathQuantity;

    /**
     *
     */
    private Integer feedType;

    /**
     *
     */
    private String otherFeedType;

    /**
     *
     */
    private Double feedQuantity;

    /**
     *
     */
    private Integer feedUnit;

    /**
     *
     */
    private String manufacturer;

    /**
     *
     */
    private Date manufacturerDate;

    /**
     *
     */
    @JsonIgnore
    private String photoJson;

    /**
     *
     */
    private Map<String , Map<String , Object>> photo;

    /**
     * 状态 0 、正常 1、生效
     */
    private Integer status;

    /**
     * 单据是否有效
     */
    private Integer deleteFlag;

    /**
     * 创建时间
     */
    private Date createdAt;

    /**
     * 更新时间
     */
    private Date updatedAt;


    public void setPhotoJson(String photoJson){
        this.photoJson = photoJson;

        if(Strings.isNullOrEmpty(photoJson)){
            this.photo = null;
        }else {
            try {
                this.photo = objectMapper.readValue(photoJson, new TypeReference<Map<String , Map<String , Object>>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }

    public void setPhoto(Map<String , Map<String , Object>> photo){
        this.photo = photo;

        if(photo == null || photo.isEmpty()){
            this.photoJson = null;
        }else {
            try {
                this.photoJson = objectMapper.writeValueAsString(photo);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }

}




